package com.ruoyi.chat.ws;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChatController {
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    ObjectMapper objectMapper = new ObjectMapper();

    //客户端发送消息入口，群发消息
    @MessageMapping("/chat/sendMessage")
    public void sendMessage(@Payload MessageVo chatMessage) throws JsonProcessingException {
        System.out.println("sendMessage" + chatMessage);
        if (StringUtils.hasText(chatMessage.getGroupName())) {//群聊
            rabbitTemplate.convertAndSend("stomp_group",
                    "group." + chatMessage.getGroupName(), objectMapper.writeValueAsString(chatMessage));
        } else if (StringUtils.hasText(chatMessage.getPullName())) {//单发
            rabbitTemplate.convertAndSend("stomp_private",
                    "private." + chatMessage.getPullName(), objectMapper.writeValueAsString(chatMessage));
        } else {//广播信息
            rabbitTemplate.convertAndSend("stomp",
                    "", objectMapper.writeValueAsString(chatMessage));
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(),
            exchange = @Exchange(value = "stomp", type = ExchangeTypes.FANOUT),
            key = ""))
    @RabbitHandler
    //广播信息监听
    public void groupMessage(String message) throws JsonProcessingException {
        MessageVo messageVo = objectMapper.readValue(message, MessageVo.class);

        messagingTemplate.convertAndSend("/topic/public", messageVo);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(),
            exchange = @Exchange(value = "stomp_group", type = ExchangeTypes.TOPIC),
            key = "group.*"))
    @RabbitHandler
    //群发信息监听
    public void group(String message) throws JsonProcessingException {
        MessageVo messageVo = objectMapper.readValue(message, MessageVo.class);
        messagingTemplate.convertAndSend("/group/"+messageVo.getGroupName(), messageVo);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(),
            exchange = @Exchange(value = "stomp_private", type = ExchangeTypes.TOPIC),
            key = "private.*"))
    @RabbitHandler
    //单发信息监听
    public void one(String message) throws JsonProcessingException {
        MessageVo messageVo = objectMapper.readValue(message, MessageVo.class);
        messagingTemplate.convertAndSendToUser(messageVo.getPullName(),"/private", messageVo);
    }

}
